/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jdSeguridadAdministracionLogueo.java
 *
 * Created on 13/05/2010, 09:58:01 AM
 */

package gestionseguridadadministrativacliente;

import administracion.delegate.GestionSeguridadDelegate;
import administracion.helper.Listas;
import administracion.helper.Utiles_MensajesDialogo;
import administracion.propiedades.Propiedades;
import gestionseguridadadministrativa.tareasprogramadas.EstadosActuales;
import gestionseguridadadministrativa.tareasprogramadas.TemporizadorTareas;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;

import suministros.ejb30.TO.AlertaTO;
import suministros.ejb30.TO.EmpresaTO;
import suministros.ejb30.TO.ZonaTO;


/**
 *
 * @author Renato
 */
public class jdSeguridadAdministracionLogueo extends javax.swing.JDialog {
    GestionSeguridadAdministrativaclienteApp appPrincipal;
    boolean cargarSistemaPrincipal;
    String usuario;
    Boolean estado=false;
    /** Creates new form jdSeguridadAdministracionLogueo */
    public jdSeguridadAdministracionLogueo(java.awt.Frame parent, boolean modal,boolean cargarSistema) {
        super(parent, modal);
        cargarSistemaPrincipal=cargarSistema;
        initComponents();
    }
    public jdSeguridadAdministracionLogueo(GestionSeguridadAdministrativaclienteApp app, boolean modal,boolean cargarSistema) {
    super(app.getMainFrame(), modal);
    this.appPrincipal = app;
    cargarSistemaPrincipal=cargarSistema;
    initComponents();
  }
    public jdSeguridadAdministracionLogueo(GestionSeguridadAdministrativaclienteApp app, boolean modal,boolean cargarSistema, String user) {
    super(app.getMainFrame(), modal);
    this.appPrincipal = app;
    cargarSistemaPrincipal=cargarSistema;
    this.usuario=user;
    this.estado=true;
    initComponents();
    this.jtfNomUsuario.setText(usuario);
    this.jtfNomUsuario.setEnabled(false);
  }
     public void correrAlerta(){
         try {
            int tamaño=Listas.obtenerAlertasTamaño();
            String alerta= "Usted tiene "+ tamaño+ " nuevas alertas";
            Toaster toasterManager = new Toaster();
            toasterManager.showToaster( new ImageIcon(appPrincipal.getClass().getResource("/gestionseguridadadministrativacliente/resources/alerta.png")),alerta );         
        } catch (Exception ex) {
            ex.printStackTrace();
        }
     }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtfNomUsuario = new javax.swing.JTextField();
        jpfPassUsuario = new javax.swing.JPasswordField();
        jbAceptar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gestionseguridadadministrativacliente.GestionSeguridadAdministrativaclienteApp.class).getContext().getResourceMap(jdSeguridadAdministracionLogueo.class);
        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setForeground(resourceMap.getColor("jLabel2.foreground")); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jtfNomUsuario.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jtfNomUsuario.setText(resourceMap.getString("jtfNomUsuario.text")); // NOI18N
        jtfNomUsuario.setName("jtfNomUsuario"); // NOI18N
        jtfNomUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfNomUsuarioActionPerformed(evt);
            }
        });

        jpfPassUsuario.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jpfPassUsuario.setText(resourceMap.getString("jpfPassUsuario.text")); // NOI18N
        jpfPassUsuario.setName("jpfPassUsuario"); // NOI18N
        jpfPassUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jpfPassUsuarioActionPerformed(evt);
            }
        });

        jbAceptar.setFont(resourceMap.getFont("jbAceptar.font")); // NOI18N
        jbAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_ok.png"))); // NOI18N
        jbAceptar.setMnemonic('A');
        jbAceptar.setText(resourceMap.getString("jbAceptar.text")); // NOI18N
        jbAceptar.setName("jbAceptar"); // NOI18N
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });
        jbAceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbAceptarKeyPressed(evt);
            }
        });

        jbCancelar.setFont(resourceMap.getFont("jButton1.font")); // NOI18N
        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbCancelar.setMnemonic('C');
        jbCancelar.setText(resourceMap.getString("jbCancelar.text")); // NOI18N
        jbCancelar.setName("jbCancelar"); // NOI18N
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addGap(32, 32, 32)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtfNomUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jpfPassUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jbAceptar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jbCancelar)))
                        .addGap(50, 50, 50))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbAceptar, jbCancelar});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jpfPassUsuario, jtfNomUsuario});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jtfNomUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jpfPassUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbCancelar)
                            .addComponent(jbAceptar))))
                .addGap(11, 11, 11))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed

      try {
      String user = "";
      String pass = "";
      user = this.jtfNomUsuario.getText().trim();
      pass = this.jpfPassUsuario.getText().trim();

      if(user.trim().equalsIgnoreCase("") || pass.trim().equalsIgnoreCase("")){
          Utiles_MensajesDialogo.mensajeError("Los campos no pueden estar vacios!");
          return;
      }
      GestionSeguridadDelegate gestion = GestionSeguridadDelegate.getInstance();
      boolean entrar = gestion.loguear(user, pass);
      if (entrar) {
       

        Propiedades.setUsuario(user);
        Propiedades.setEstado("1");
       
        dispose();
        EstadosActuales.setEstadoDialogoLogueo(true);
        if(this.cargarSistemaPrincipal){
            
             HiloIniSist hilo = new HiloIniSist( );
             hilo.start();
             TemporizadorTareas.iniciar();
             
            
        }
        //this.appPrincipal.show(new GestionSeguridadAdministrativaclienteView(this.appPrincipal));
        

      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    }//GEN-LAST:event_jbAceptarActionPerformed

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
       
        System.exit(0);
    }//GEN-LAST:event_jbCancelarActionPerformed

    private void jtfNomUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfNomUsuarioActionPerformed
        
        this.jpfPassUsuario.requestFocus();
    }//GEN-LAST:event_jtfNomUsuarioActionPerformed

    private void jpfPassUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jpfPassUsuarioActionPerformed
       
        this.jbAceptar.requestFocus();
    }//GEN-LAST:event_jpfPassUsuarioActionPerformed

    private void jbAceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbAceptarKeyPressed
        
        
    }//GEN-LAST:event_jbAceptarKeyPressed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        
                jdSeguridadAdministracionLogueo dialog = new jdSeguridadAdministracionLogueo(new javax.swing.JFrame(), true,true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
      
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton jbAceptar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JPasswordField jpfPassUsuario;
    private javax.swing.JTextField jtfNomUsuario;
    // End of variables declaration//GEN-END:variables

    class HiloIniSist extends Thread{
        HiloIniSist(  ){


        }
        public void run(){
            try {
                Listas.cargarListas();
                correrAlerta();
                appPrincipal.show(new GestionSeguridadAdministrativaclienteView(appPrincipal));
                //correrAlerta();
            } catch (Exception ex) {
                Logger.getLogger(jdSeguridadAdministracionLogueo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        private String nomb;
        private String id;
        private String tipoUser;
    }
}
